/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlcommand;

import data.ManagerDatabase;
import data.Row;
import java.util.ArrayList;

/**
 *
 * @author Quang
 */
public  class Command {
    
    protected  String sql;
    protected MyError err;   
    protected String type;
    protected ManagerDatabase manager;
    protected static String[] typeSQLcommand = {
        "INSERT",
        "DELETE",
        "UPDATE",
        "SELECT"    
    };
    protected  static String[] keyWordSQL = {
        "FROM",
        "SELECT",        
        "CREATE",
        "DATABASE",
        "TABLE",
        "WHERE",
        "UPDATE",
        "SET",
        "DELETE",
        "INSERT",
        "INTO",
        "VALUES",
        "AND",
        "OR",
        "NOT"
    };
    protected static String[] keyWordConnect = {
        "AND",
        "OR",
        "NOT"                 
    };
    protected static String[] keyWordOperator = {
        ">",
        ">=",
        "<",
        "<=",
        "=",
        "<>",
             
    };
    public Command(String strSQL,ManagerDatabase md){
        sql = Command.standardizeString(strSQL);
        manager = md;
    }
    public Command getCommand(MyError err){
        return null;
    }
    public  ArrayList<Row> excute(MyError err){
        return null;
    }
    
    /**
     * @return the sql
     */
    public String getSql() {
        return sql;
    }

    /**
     * @param sql the sql to set
     */
    public void setSql(String sql) {
        this.sql = standardizeString(sql);
    }
    public  static String standardizeString(String str) {
        str = str.toUpperCase();
        StringBuffer s = new StringBuffer(str);
        String kq = s.toString();
        // xoa ki tu dau dong
        int i =0;
        while(s.charAt(i) == ' ' && s != null) {
            s.deleteCharAt(i);            
        }
        //xoa khoang trang cuoi dong
        i = s.length()-1;
        while(s.charAt(i)== ' '&& i>=0){
            s.deleteCharAt(i);
            i--;
        }
        
        // xoa khoang trang du
        for(i=0; i<s.length(); i++){
            if(s.charAt(i) == ' '&& s.charAt(i+1) == ' '){
                s.deleteCharAt(i);
                i--;
            }
        }
//        // them khoang trang giua cac tu khoa
//        for(i=0; i<keyWordSQL.length; i++){
//            int posKey = s.indexOf(keyWordSQL[i]);
//            int posComm = posKey + keyWordSQL[i].length();
//            if(posKey >0 &&  s.charAt(posKey-1) != ' '){
//                s.insert(posKey , ' ');
//            }
//            posKey = s.indexOf(keyWordSQL[i]);
//            posComm = posKey + keyWordSQL[i].length();
//            if(posKey >=0 &&  s.charAt(posComm) != ' '){
//                s.insert(posComm , ' ');
//            }
//        }
        // them khoang trang giua cac toan tu
         for(i=0; i<keyWordOperator.length; i++){
            int posKey = s.indexOf(keyWordOperator[i]);
            int posComm = posKey + keyWordOperator[i].length();          
    
            if(posKey >0 &&  s.charAt(posKey-1) != ' '){
                s.insert(posKey , ' ');
            }
            posKey = s.indexOf(keyWordOperator[i]);
            posComm = posKey + keyWordOperator[i].length();
            if(posKey >=0 &&  s.charAt(posComm) != ' '){
                s.insert(posComm , ' ');
            }
            // neu la toan tu dang <>, <=, >= xoa khoang tang o giua
            if(s.charAt(posKey+2)=='='&& keyWordOperator[i].length() ==1){
                    if(s.charAt(posKey+1)== ' ')
                        s.deleteCharAt(posKey+1);                    
                }
                else if(s.charAt(posKey+2)=='>'&& keyWordOperator[i].length() ==1){
                    if(s.charAt(posKey+1)== ' ')
                        s.deleteCharAt(posKey+1);                    
                }
        }
         // xoa khoang trang truoc dau phay (','), them khoang trang sau dau phay
        for (i = 0; i < s.length()-1; i++) {
            if (s.charAt(i) == ',' && s.charAt(i - 1) == ' ') {
                s.deleteCharAt(i - 1);
            }
            if (s.charAt(i) == ',' && s.charAt(i + 1) != ' ') {
                s.insert(i + 1, ' ');
            }
            // xoa khoang trang truoc va sau dau ngoac don ')'
            if(s.charAt(i)==')'&&s.charAt(i+1)==' ')
                s.deleteCharAt(i+1);
            if(s.charAt(i)==')'&&s.charAt(i-1)==' ')
                s.deleteCharAt(i-1);
            
             if(s.charAt(i)=='('&&s.charAt(i+1)==' ')
                s.deleteCharAt(i+1);
//            if(s.charAt(i)=='('&&s.charAt(i-1)!=' ')
//                s.insert(i-1, ' ');
        }
        kq = s.toString();        
        return kq.toUpperCase();
    }
    
    
      public  static String newStandardizeString(String str) {
        str = str.toUpperCase();
        StringBuffer s = new StringBuffer(str);
        String kq = s.toString();
        // xoa ki tu dau dong
        int i =0;
        while(s.charAt(i) == ' ' && s != null) {
            s.deleteCharAt(i);            
        }
        //xoa khoang trang cuoi dong
        i = s.length()-1;
        while(s.charAt(i)== ' '&& i>=0){
            s.deleteCharAt(i);
            i--;
        }
        
        // xoa khoang trang du
        for(i=0; i<s.length(); i++){
            if(s.charAt(i) == ' '&& s.charAt(i+1) == ' '){
                s.deleteCharAt(i);
                i--;
            }
        }

        // them khoang trang giua cac toan tu
         for(i=0; i<keyWordOperator.length; i++){
            int posKey = s.indexOf(keyWordOperator[i]);
            int posComm = posKey + keyWordOperator[i].length();          
    
            if(posKey >0 &&  s.charAt(posKey-1) != ' '){
                s.insert(posKey , ' ');
            }
            posKey = s.indexOf(keyWordOperator[i]);
            posComm = posKey + keyWordOperator[i].length();
            if(posKey >=0 &&  s.charAt(posComm) != ' '){
                s.insert(posComm , ' ');
            }
            // neu la toan tu dang <>, <=, >= xoa khoang tang o giua
            if(s.charAt(posKey+2)=='='&& keyWordOperator[i].length() ==1){
                    if(s.charAt(posKey+1)== ' ')
                        s.deleteCharAt(posKey+1);                    
                }
                else if(s.charAt(posKey+2)=='>'&& keyWordOperator[i].length() ==1){
                    if(s.charAt(posKey+1)== ' ')
                        s.deleteCharAt(posKey+1);                    
                }
        }
         // xoa khoang trang truoc dau phay (','), them khoang trang sau dau phay
        for (i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ',' && s.charAt(i - 1) == ' ') {
                s.deleteCharAt(i - 1);
            }
            if (s.charAt(i) == ',' && s.charAt(i + 1) != ' ') {
                s.insert(i + 1, ' ');
            }
        }
        kq = s.toString();        
        return kq.toUpperCase();
    }
      public static String ChuanHoa(String str){
           str = str.toUpperCase();
        StringBuffer s = new StringBuffer(str);
       // String kq = s.toString();
        // xoa ki tu dau dong
        int i =0;
        while(s.charAt(i) == ' ' && s != null) {
            s.deleteCharAt(i);            
        }
        //xoa khoang trang cuoi dong
        i = s.length()-1;
        while(s.charAt(i)== ' '&& i>=0){
            s.deleteCharAt(i);
            i--;
        }
        
        // xoa khoang trang du
        for(i=0; i<s.length(); i++){
            if(s.charAt(i) == ' '&& s.charAt(i+1) == ' '){
                s.deleteCharAt(i);
                i--;
            }
        }
        return s.toString();
      }
    /**
     * phan tich chuoi dang left >= right => ra thanh cac doi tuong dieu kien
     * @param s
     * @return 
     */
     public SingleCondition analysisCondition(String s){        
        StringBuffer str = new StringBuffer(s);
        int pos = -1;
        String opp = null;
        for (int i = 0; i < keyWordOperator.length; i++) {
            pos = str.indexOf(keyWordOperator[i]);
            if (pos != -1) {
                opp = keyWordOperator[i];
                break;
            }
        }
        if (pos == -1)
            return null;
        String left = str.substring(0, pos-1);
        String right = str.substring(pos+opp.length()+1);
        SingleCondition c = new SingleCondition(left, opp, right);
        return c;
    }
     /**
      * tra ve vi tri cua str trong sub
      * @param s
      * @param str
      * @return -1: ko ton tai, 
      */
    public static int indexOfString(StringBuffer sub, String str){
        int pos;
        int subLength = sub.length();
        int strLength = str.length();
        for (int i = 0; i < sub.length(); i++) {
            pos = sub.indexOf(str, i);
            if (pos > 0 && pos + strLength == subLength && sub.charAt(pos - 1) == ' ') {
                return pos;
            } else if (pos > 0
                    && sub.charAt(pos - 1) == ' '
                    && (sub.charAt(pos + str.length()) == ' ')) {
                return pos;
            } else if ((pos == 1 && sub.charAt(0) == '(') && (sub.charAt(pos + str.length()) == ' ')) {
                return pos;
            } else if (pos == 0 && sub.charAt(pos + str.length()) == ' ') {
                return pos;
            }

        }
        return -1;
        
    }
    public static int indexOfString(String sub, String str){
        return indexOfString(new StringBuffer(sub), str);
    }

}
